package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TourismPersonnel;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TourismRecord;
import com.ruoyi.system.service.ITourismRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 预约人Controller
 *
 * @author gqd
 * @date 2021-02-04
 */
@RestController
@RequestMapping("/system/record")
public class TourismRecordController extends BaseController
{
    @Autowired
    private ITourismRecordService tourismRecordService;
    @Resource
    private TokenService tokenService;

    /**
     * 查询预约人列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(TourismRecord tourismRecord)
    {
        startPage();
        List<TourismRecord> list = tourismRecordService.selectTourismRecordList(tourismRecord);
        return getDataTable(list);
    }

    /**
     * 导出预约人列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "预约人", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TourismRecord tourismRecord)
    {
        List<TourismRecord> list = tourismRecordService.selectTourismRecordList(tourismRecord);
        ExcelUtil<TourismRecord> util = new ExcelUtil<TourismRecord>(TourismRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 获取预约人详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tourismRecordService.selectTourismRecordById(id));
    }

    /**
     * 新增预约人
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "预约人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TourismRecord tourismRecord)
    {
        return toAjax(tourismRecordService.insertTourismRecord(tourismRecord));
    }

    /**
     * 修改预约人
     */
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "预约人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TourismRecord tourismRecord)
    {
        return toAjax(tourismRecordService.updateTourismRecord(tourismRecord));
    }

    /**
     * 删除预约人
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "预约人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tourismRecordService.deleteTourismRecordByIds(ids));
    }

    @PostMapping("/selectList")
    public AjaxResult selectSpotById( HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(loginUser != null){
            Integer userId = loginUser.getUserId();
            return tourismRecordService.FindAll(userId);
        }else {
            return AjaxResult.error(201, "您还未注册或者登录，请先注册或登录");
        }

    }
}
